package net.alan.etl.impl;

import java.util.TimerTask;

import net.alan.etl.FlowContext;
import net.alan.etl.i.IFlowTask;
import net.alan.etl.kafka.FlowParams;

/**
 * @author 李岩飞
 * @email eliyanfei@126.com
 * @date 2018年5月10日下午2:17:43
 */
public abstract class AbstractFlowTask extends TimerTask implements IFlowTask {
	protected FlowParams params;// 参数

	@Override
	public final void run() {
		try {
			long t = System.currentTimeMillis();
			executeTask();
			FlowContext.createContext()
					.print("TASK " + taskId() + " use times " + (System.currentTimeMillis() - t) / 1000 + " seconds");
			nextTask();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
		}
	}

	private void nextTask() {
		FlowParams params = new FlowParams();
		params.setTaskId(taskId());
		if (monitorTaskId() == null) {
			params.setNo(String.valueOf(System.currentTimeMillis()));
		} else {
			params.setNo(this.params.getNo());
		}
		FlowContext.createContext().send(params);
	}

	/**
	 * 任务执行过程
	 */
	public abstract void executeTask();

	@Override
	public void initParam(FlowParams params) {
		this.params = params;
	}

	/**
	 * 定时器
	 */
	public boolean timer() {
		return false;
	}
}
